package com.zhengshang.quartz.task;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zhengshang.api.domain.AppGasBrandType;
import com.zhengshang.api.domain.AppGasStation;
import com.zhengshang.api.service.IAppGasBrandTypeService;
import com.zhengshang.api.service.IAppGasStationService;
import com.zhengshang.common.constant.app.AppConstant;
import com.zhengshang.common.utils.DateUtils;
import com.zhengshang.common.utils.http.HttpUtils;
import com.zhengshang.common.utils.sign.SignMD5Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.TreeMap;

/**
 * 定时任务调度测试
 * 
 * @author ruoyi
 */
@Component("gasTask")
public class GasTask
{
    protected final Logger logger = LoggerFactory.getLogger(GasTask.class);
    @Autowired
    private IAppGasStationService appGasStationService;
    @Autowired
    private IAppGasBrandTypeService appGasBrandTypeService;

    /**
     * 更新站点信息
     */
    public void queryGasInfoListOilNoNew()
    {
        logger.info("<gas.queryGasInfoListOilNoNew>开始执行");
        TreeMap<String, String> param = new TreeMap<>();
        param.put("app_key", AppConstant.GAS_APP_KEY);
        param.put("timestamp", DateUtils.getNowDate().getTime() + "");
        param.put("channelId", AppConstant.GAS_CHANNEL_ID);
        param.put("sign", SignMD5Util.getSignNL(param, AppConstant.GAS_APP_SECRET));
        String result = HttpUtils.sendPost(AppConstant.GAS_QUERY_GASINFOLIST_OILNONEW_URL, HttpUtils.getParam(param));
        JSONObject resultObj = JSONObject.parseObject(result);
//        logger.info("<gas.queryGasInfoListOilNoNew>返回数据为：{}", resultObj);
        if (resultObj.getIntValue("code") == 200) {
            JSONArray dataArr = resultObj.getJSONArray("result");
            for (int i = 0; i < dataArr.size(); i++) {
                JSONObject gasObj = dataArr.getJSONObject(i);
                AppGasStation appGasStation = new AppGasStation();
                appGasStation.setGasId(gasObj.getString("gasId"));
                appGasStation.setGasName(gasObj.getString("gasName"));
                appGasStation.setGasTypeId(gasObj.getString("gasType"));
                appGasStation.setGasLogoBig(gasObj.getString("gasLogoBig"));
                appGasStation.setGasLogoSmall(gasObj.getString("gasLogoSmall"));
                appGasStation.setGasAddress(gasObj.getString("gasAddress"));
                appGasStation.setGasAddressLongitude(gasObj.getDoubleValue("gasAddressLongitude"));
                appGasStation.setGasAddressLatitude(gasObj.getDoubleValue("gasAddressLatitude"));
                appGasStation.setProvinceCode(gasObj.getString("provinceCode"));
                appGasStation.setProvinceName(gasObj.getString("provinceName"));
                appGasStation.setCityCode(gasObj.getString("cityCode"));
                appGasStation.setCityName(gasObj.getString("cityName"));
                appGasStation.setCountyCode(gasObj.getString("countyCode"));
                appGasStation.setCountyName(gasObj.getString("countyName"));
                appGasStation.setIsInvoice(gasObj.getIntValue("isInvoice"));
                appGasStation.setCreateBy("admin");
                appGasStationService.insertAppGasStation(appGasStation);
            }
            logger.info("<gasTask.queryGasInfoListOilNoNew>自动更新{}条油站数据", dataArr.size());
        } else {
            logger.error("<gasTask.queryGasInfoListOilNoNew>自动更新油站数据失败:{}", resultObj);
        }
    }

    /**
     * 更新站点品牌信息
     */
    public void gasBrand()
    {
        logger.info("<gas.gasBrand>开始执行");
        TreeMap<String, String> param = new TreeMap<>();
        param.put("app_key", AppConstant.GAS_APP_KEY);
        param.put("channelId", AppConstant.GAS_CHANNEL_ID);
        param.put("sign", SignMD5Util.getSignNL(param, AppConstant.GAS_APP_SECRET));
        String result = HttpUtils.sendPost(AppConstant.GAS_BRAND_TYPE_URL, HttpUtils.getParam(param));
        JSONObject resultObj = JSONObject.parseObject(result);
        logger.info("<gas.gasBrand>返回数据为：{}", resultObj);
        if (resultObj.getIntValue("code") == 200) {
            JSONArray dataArr = resultObj.getJSONArray("result");
            for (int i = 0; i < dataArr.size(); i++) {
                JSONObject gasObj = dataArr.getJSONObject(i);
                AppGasBrandType appGasBrandType = new AppGasBrandType();
                appGasBrandType.setGasBrandType(gasObj.getString("gasBrandType"));
                appGasBrandType.setGasBrandName(gasObj.getString("gasBrandName"));
                appGasBrandTypeService.insertAppGasBrandType(appGasBrandType);
            }
            logger.info("<gasTask.gasBrand>自动更新{}条油站品牌数据", dataArr.size());
        }
    }

    /**
     * 清除已下线站点信息
     */
    public void offlineGas()
    {
        logger.info("<gas.offlineGas>开始执行");
        appGasStationService.deleteOfflineAppGasStation();
    }

}
